package com.asyx.trial.process;

import org.apache.commons.cli.BasicParser;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class MainProcess {
	private static Logger log = LoggerFactory.getLogger(MainProcess.class);
	public static void main(String args[]){

		// create the Options object
		Options options = new Options();
		// add the individual options to it by two different ways
		// 1st way is by calling the addOption method on Options object
		options.addOption("n", true, "[name] your name");
		// 2nd way is by creating an Option and then adding it
		Option timeOption = new Option("t", false, "current time");
//		timeOption.setRequired(true);
		options.addOption(timeOption);
		// now lets parse the input
		CommandLineParser parser = new BasicParser();
		CommandLine cmd;
		try{
			cmd = parser.parse(options, args);
		}catch (ParseException pe){ usage(options); return; }
		if(cmd.hasOption("n")){
			String name  = cmd.getOptionValue("n");
			String[] args1 = cmd.getArgs();
			log.debug("Logging = "+name);
			log.info("Nice to meet you = " + name);
			log.info("ARGUMENTS : "+args1.length);
			int i = 0;
			for (String arg : args1){
	
				log.info("arg "+i+" : "+arg);
				i++;
			}
			
		}
		// now lets interrogate the options and execute the relevant parts
		if(cmd.hasOption("t")){
			log.info("Date/Time: " + new java.util.Date());
		}
		

	}

	private static void usage(Options options){

		// Use the inbuilt formatter class
		HelpFormatter formatter = new HelpFormatter();
		formatter.printHelp( "CLIDemo", options );
	}
}
